package com.starmark.gateway.project.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.starmark.core.framework.base.AbstractBaseService;
import com.starmark.gateway.project.provider.entity.GatewayProjectAdmin;
import com.starmark.gateway.project.provider.mapper.GatewayProjectAdminMapper;
import com.starmark.gateway.project.provider.service.IGatewayProjectAdminService;
import com.starmark.gateway.project.provider.vo.AuthOrgElementDto;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author starmark
 * @since 2020-07-28
 */
@Service
public class GatewayProjectAdminServiceImpl extends AbstractBaseService<GatewayProjectAdminMapper, GatewayProjectAdmin> implements IGatewayProjectAdminService {


    @Override
    public void buildPageSearch(Map<String, Object> params, QueryWrapper<GatewayProjectAdmin> eWrapper) {
        if (params.get("keyword") != null && StrUtil.isNotEmpty(params.get("keyword") + "")) {
            eWrapper.like("name", params.get("keyword") + "");
        }

    }

    @Override
    public void deleteProjectAdmins(String projectId) {
        Assert.hasText(projectId,"工程ID不允许为空");
        this.baseMapper.deleteProjectAdmins(projectId);
    }

    @Override
    public List<AuthOrgElementDto> findAdminsByProjectId(String projectId) {
        Assert.hasText(projectId,"工程ID不允许为空");
        QueryWrapper<GatewayProjectAdmin> eWrapper = new QueryWrapper<>();
        eWrapper.eq("project_id",projectId);
        List<GatewayProjectAdmin> admins=this.list(eWrapper);
        return admins.stream().map(admin->new AuthOrgElementDto(admin.getAdminId(),admin.getAdminName())).collect(Collectors.toList());
    }
}
